<?php
require_once './cc.php';

$uid = $_REQUEST['uid'];
$gid = $_REQUEST['gid'];
$img = $_REQUEST['img'];

$name = $_REQUEST['name1'];
$price = $_REQUEST['price'];
$count2 = $_REQUEST['count'];

$sql = "SELECT * FROM userinfo WHERE gid = '$gid' AND name = '$name'";

$result = mysqli_query($conn, $sql);

$data = mysqli_fetch_assoc($result);

$array = array(
    'code' => 0,
    'data' => false,
    'msg' => '失败',
);

if ($data) {
    $id = $data['id'];

    $count = $data['count'] ;

    $sql = "UPDATE userinfo SET count = $count WHERE id = $id";

    mysqli_query($conn, $sql);

    $rows = mysqli_affected_rows($conn);

    if ($rows > 0) {
        $array = array(
            'code' => 1,
            'data' => true,
        );
    }

} else {

    $sql = "INSERT INTO userinfo (uid, gid,img ,name,price,count) VALUES ('$uid','$gid','$img','$name','$price','$count2')";

    mysqli_query($conn, $sql);

    $rows = mysqli_affected_rows($conn);

    if ($rows > 0) {
        $array = array(
            'code' => 1,
            'data' => true,
        );
    }
}
$sql2 = "SELECT * FROM userinfo WHERE  name = '$name'";

$result2 = mysqli_query($conn, $sql2);

while($data2 = mysqli_fetch_assoc($result2)){
    $array2[] = $data2;
    // echo json_encode($array);
}

if($result2){
    $array = array(
        'code' => 1,
        'data' => true,
        'data2' =>$array2,
    );

}

echo json_encode($array);










